/**********************************************************************************************************************
 * \file SMU_IR_Alarm.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 *
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
 * derivative works of the Software, unless such copies or derivative works are solely in the form of
 * machine-executable object code generated by a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *********************************************************************************************************************/
 
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include <smu/drv_smu.h>
#include "Ifx_Types.h"
#include "IfxSmu.h"
#include "IfxSrc.h"
#include "IfxPort.h"
#include "drv_led.h"

/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define ISR_PRIORITY_SMU_INT0   10                          /* Define the SMU Service Request 0 interrupt priority  */
#define LED                     &MODULE_P13,0               /* LED which is turned on in Interrupt Service Routine  */

/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* Macro to define Interrupt Service Routine */
IFX_INTERRUPT(ISR_SMU_Alarm, 0, ISR_PRIORITY_SMU_INT0);

/* Interrupt Service Routine of SMU, gets triggered when the SMU Software Alarm 0 is triggered */
void ISR_SMU_Alarm(void)
{
    IfxSmu_clearAlarmStatus(IfxSmu_Alarm_Software_Alarm0);              /* Clear alarm status flag                  */
    IfxSmu_clearAlarmExecutedStatus(IfxSmu_AlarmExecutionStatus_irq0);  /* Clear Alarm Execution Status register bit*/

//    IfxPort_setPinState(LED, IfxPort_State_low);                        /* Turn on LED (LED is low-level active)    */

    drv_led2_trun_on();
}

/* This function configures the SMU module to trigger an interrupt if a software alarm occurs */
void SMU_configuration(void)
{
    IfxPort_setPinState(LED, IfxPort_State_high);                       /* Turn off LED (LED is low-level active)   */
    IfxPort_setPinMode(LED, IfxPort_Mode_outputPushPullGeneral);        /* Initialize LED port pin                  */

    /* Enable the SMU register configuration by setting the CFGLCK field of the SMU_KEYS register to 0xBC */
    IfxSmu_unlockConfigRegisters();

    /* Clear Safety ENDINIT protection for modification of the configuration registers */
    IfxScuWdt_clearSafetyEndinit(IfxScuWdt_getSafetyWatchdogPasswordInline());
    /* Set the IGCS0 field of SMU_AGC register to 1 to trigger SMU Interrupt Request 0 */
    MODULE_SMU.AGC.B.IGCS0 = 1;
    /* Set Safety ENDINIT protection after modification of the configuration registers */
    IfxScuWdt_setSafetyEndinit(IfxScuWdt_getSafetyWatchdogPasswordInline());

    /* SMU alarm configuration: select Software Alarm 0 (alarm group 10) and send the interrupt request to the
     * interrupt system according to the Interrupt Generation Configuration Set 0 (IGCS0)
     */
    IfxSmu_setAlarmAction(IfxSmu_Alarm_Software_Alarm0, IfxSmu_InternalAlarmAction_igcs0);

    /* Afterwards, lock the SMU configuration */
    IfxSmu_lockConfigRegisters();

    /* Service request configuration */
    /* Get source pointer of SMU Service Request 0 in order to initialize and enable it */
    volatile Ifx_SRC_SRCR *src = &SRC_SMU0;
    IfxSrc_init(src, IfxSrc_Tos_cpu0, ISR_PRIORITY_SMU_INT0);
    IfxSrc_enable(src);

    IfxSmu_activateRunState();                                          /* Start the SMU state machine (SSM)        */
}

/* This function triggers the Software Alarm 0 */
void trigger_SMU_alarm(void)
{
    IfxSmu_setAlarmStatus(IfxSmu_Alarm_Software_Alarm0);                /* Trigger Software Alarm 0                 */
}

